package com.hot100;

import java.util.PriorityQueue;

public class Solution236 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> {
            if (o1[0] != o2[0]) return Integer.compare(o2[0], o1[0]);
            else return Integer.compare(o2[1], o1[1]);
        });
        for (int i = 0; i < k; i++) {
            pq.add(new int[]{nums[i], i});
        }
        int[] res = new int[nums.length - k + 1];
        res[0] = pq.peek()[0];
        for (int i = k; i < nums.length; i++) {
            pq.add(new int[]{nums[i], i});
            while (pq.peek()[1] <= i - k) pq.poll();
            res[i - k + 1] = pq.peek()[0];
        }
        return res;
    }
}
